/**
 * 存储源
 * @author luguoxiang
 * @date 2024-03-01
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
import { FormItemProps } from 'tdesign-vue-next';
import { BaseEntity, IConfigSchema } from '@/soar';
import { basePath } from './api';
import { MenuComponent } from '@/types/interface';
import { baseURL } from '@/config/global';

export interface StorageSource extends BaseEntity {
  /** id */
  id?: any;
  /** 名称 */
  name?: any;
  /** 状态（0正常，1禁用，2只读，3只写） */
  status?: any;
  /** 类型（0本地，1OSS） */
  type?: any;
  /** 资源地址 */
  endpoint?: any;
  /** Access key */
  accessKey?: any;
  /** Secret key */
  secretKey?: any;
  /** 最大线程数 */
  maxConnections?: any;
  /** pathStyle模式 */
  pathStyleAccess?: any;
  /** 区域 */
  region?: string;
  /** 备注 */
  remark?: string;
  /** 排序 */
  sort?: number;
  /** 桶名称 */
  bucketName?: string;
}

const fieldList: IConfigSchema<StorageSource>[] = [
  {
    field: 'type',
    label: '类型',
    dataType: 'dict',
    params: 'FileStorageTypeEnum',
    table: {
      width: 120,
    },
    form: {
      rules: true,
      disabled({ isUpdate }) {
        return isUpdate;
      },
    },
  },
  {
    field: 'name',
    label: '名称',
    dataType: 'string',
    form: {
      rules: true,
    },
  },
  {
    field: 'endpoint',
    label: '资源地址',
    dataType: 'string',
    form: {
      rules: true,
      slots: 'endpoint',
    },
  },
  {
    field: 'bucketName',
    label: '桶名称',
    dataType: 'string',
    form: {
      rules: true,
      slots: {
        label: 'bucketNameLabel',
      },
      componentProps: {
        placeholder: '请输入',
      },
    },
  },
  {
    field: 'accessKey',
    label: '账号',
    dataType: 'string',
    table: false,
    search: false,
    form: {
      slots: 'accessKey',
      rules: true,
      formItemProps({ data }) {
        if (data.type === 0) {
          return {
            help: '需在根路径下放置与账号同名的文件，防止随意设置上传目录',
          };
        }
        return {};
      },
    },
  },
  {
    field: 'secretKey',
    label: '密码',
    dataType: 'string',
    table: false,
    search: false,
    form: {
      slots: 'secretKey',
    },
  },
  {
    field: 'pathStyleAccess',
    label: 'pathStyle',
    dataType: 'boolean',
    form: {
      vif: ({ data }) => data.type === 1,
      formItemProps: {
        help: '如果上传不成功，试试切换这个参数',
      },
    },
    table: false,
  },
  {
    field: 'region',
    label: '区域',
    dataType: 'string',
    form: {
      vif: ({ data }) => data.type === 1,
    },
  },
  {
    field: 'maxConnections',
    label: '最大线程数',
    dataType: 'number',
    form: {
      vif: ({ data }) => data.type === 1,
    },
    table: {
      width: 95,
    },
  },
  {
    field: 'remark',
    label: '备注',
    dataType: 'string',
  },
  {
    field: 'defaulted',
    label: '默认仓库',
    dataType: 'boolean',
    table: {
      width: 80,
    },
  },
  {
    field: 'status',
    label: '状态',
    dataType: 'dict',
    params: 'FileStorageStatusEnum',
    table: {
      width: 80,
    },
    form: {
      rules: true,
    },
  },
  {
    field: 'sort',
    label: '排序',
    dataType: 'number',
    table: {
      width: 80,
    },
  },
];

export default fieldList;

/**
 * 组件配置（用于生成菜单数据）
 */
export const COMPONENTS: MenuComponent[] = [
  {
    title: '文件仓库',
    component: () => import('./List.vue'),
    sort: 50,
    keepalive: true,
    icon: 'usb',
    permits: [`GET${basePath}/query`],
    elements: [
      { title: '新增', code: 'create', permits: [`POST${basePath}/create`, `POST${basePath}/test`] },
      {
        title: '更新',
        code: 'update',
        permits: [`POST${basePath}/update`, `GET${basePath}/detail`, `POST${basePath}/test`],
      },
      { title: '删除', code: 'remove', permits: [`POST${basePath}/remove`] },
      { title: '详情', code: 'detail', permits: [`GET${basePath}/detail`] },
      { title: '导入', code: 'import', permits: [`POST${basePath}/import`] },
      { title: '导出', code: 'export', permits: [`GET${basePath}/export`] },
    ],
  },
];
